﻿namespace GWB.Core.Common.Utils
{
  public  static class ContainUtils
    {
        /// <summary>
        /// 利用反射来判断对象是否包含某个属性
        /// </summary>
        /// <param name="instance">object</param>
        /// <param name="propertyName">需要判断的属性</param>
        /// <returns>是否包含</returns>
        public static bool IsContainProperty(object instance, string propertyName)
        {
            if (instance != null && !string.IsNullOrEmpty(propertyName))
            {
                var findedPropertyInfo = instance.GetType().GetProperty(propertyName);
                return (findedPropertyInfo != null);
            }
            return false;
        }

        /// <summary>
        /// 利用反射来获取对象是否包含某个属性的值
        /// </summary>
        /// <param name="instance">object</param>
        /// <param name="propertyName">需要判断的属性</param>
        /// <returns>是否包含</returns>
        public static object GetContainPropertyValue(object instance, string propertyName)
        {
            if (instance != null && !string.IsNullOrEmpty(propertyName))
            {
                var findedPropertyInfo = instance.GetType().GetProperty(propertyName);
                if (findedPropertyInfo != null) return findedPropertyInfo.GetValue(instance);
            }
            return null;
        }
    }
}
